/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var newsProxy = new NewsProxy("https://localhost:8181/weba-novare/rs/news");

function edit(tablecell) {
    //alert(tablecell.innerHTML);
    //alert($(tablecell).closest('tr').children().eq(0).text());
    var newsDialog = new NewsDialog(tablecell.innerHTML, $(tablecell).closest('tr').children().eq(0).text(), "edit");
    newsDialog.addHandler(this.handleItem);
    newsDialog.setParent("#newsDialog");
    var windowWidth = document.documentElement.clientWidth;  
    var windowHeight = document.documentElement.clientHeight;  
    var popupHeight = $("#newsDialog").height();  
    var popupWidth = $("#newsDialog").width();  
    //centering  
    $("#newsDialog").css({  
        "position": "absolute",  
        "top": windowHeight/2-popupHeight/2,  
        "left": windowWidth/2-popupWidth/2  
    });
    newsDialog.render();
}

function add() {
    var newsDialog = new NewsDialog("", "", "add");
    newsDialog.addHandler(this.handleItem);
    newsDialog.setParent("#newsDialog");
    var windowWidth = document.documentElement.clientWidth;  
    var windowHeight = document.documentElement.clientHeight;  
    var popupHeight = $("#newsDialog").height();  
    var popupWidth = $("#newsDialog").width();  
    //centering  
    $("#newsDialog").css({  
        "position": "absolute",  
        "top": windowHeight/2-popupHeight/2,  
        "left": windowWidth/2-popupWidth/2  
    });
    newsDialog.render();
}

function handleItem(action, newsItem) {
    var deferred;
    if (action === "edit") {
        deferred = newsProxy.update(newsItem);
        deferred.done(function(){
        });
        deferred.fail(function(){
        });
    } else if (action === "add") {
        deferred = newsProxy.add(newsItem);
        deferred.done(function(){
        });
        deferred.fail(function(){
        });
    } else if (action === "del") {
        deferred = newsProxy.remove(newsItem.id);
        deferred.done(function(){
        });
        deferred.fail(function(){
        });
    }
}
